using System.Collections.Generic;
using UnityEngine;

public class ProgressManager
{
	private string progressFileName = "/progress.dat";

	private string levelsFileName = "DataFiles/levels";

	[SerializeField]
	private List<Level> levels;

	private int minLevel = 1;

	private int maxLevel;

	private int levelsPerWorld = 20;

	private int minWorldId = 1;

	private int maxWorldId;

	public ProgressManager()
	{
		levels = new List<Level>();
		InitializeLevelsProgress();
		CheckMaxLevelId();
		CreateProgressForNewLevels();
		SortLevelsByAbsoluteLevelId();
		GroupLevelsByWorlds();
		CheckAndUnlockNextLevel(updateUserPreferences: true);
		SaveLevelProgress();
	}

	public int GetMinLevelId()
	{
		return minLevel;
	}

	public int GetMaxLevelId()
	{
		return maxLevel;
	}

	public int GetMinWorldId()
	{
		return minWorldId;
	}

	public int GetMaxWorldId()
	{
		return maxWorldId;
	}

	private void CheckMaxLevelId()
	{
		int num = minLevel;
		INIParser iNIParser = new INIParser();
		TextAsset name = Resources.Load<TextAsset>(levelsFileName);
		iNIParser.Open(name);
		while (iNIParser.IsSectionExists("level_" + num.ToString()))
		{
			maxLevel = num;
			num++;
		}
		iNIParser.Close();
	}

	public void CreateProgressForNewLevels()
	{
		for (int i = minLevel; i <= maxLevel; i++)
		{
			Level level = GetLevel(i);
			if (level == null)
			{
				Level item = new Level(i);
				levels.Add(item);
			}
		}
	}

	private void SortLevelsByAbsoluteLevelId()
	{
		int count = levels.Count;
		for (int i = 1; i < count; i++)
		{
			for (int num = count - 1; num >= i; num--)
			{
				if (levels[num - 1].AbsoluteLevelId > levels[num].AbsoluteLevelId)
				{
					Level value = levels[num - 1];
					levels[num - 1] = levels[num];
					levels[num] = value;
				}
			}
		}
	}

	private void GroupLevelsByWorlds()
	{
		int num = minWorldId;
		int num2 = 1;
		for (int i = 0; i < levels.Count; i++)
		{
			levels[i].WorldId = num;
			levels[i].WorldLevelId = num2;
			num2++;
			if (num2 > levelsPerWorld)
			{
				maxWorldId = num;
				num++;
				num2 = 1;
			}
		}
	}

	private void CheckAndUnlockNextLevel(bool updateUserPreferences)
	{
		int num = -1;
		for (int num2 = levels.Count - 1; num2 >= 0; num2--)
		{
			if (levels[num2].PlayedThisLevel)
			{
				if (num2 + 1 <= levels.Count - 1)
				{
					levels[num2 + 1].IsLocked = false;
					num = num2 + 1;
				}
				break;
			}
		}
		UILevelsPicker uILevelsPicker = Object.FindObjectOfType<UILevelsPicker>();
		if (uILevelsPicker != null && num != -1)
		{
			uILevelsPicker.OnWorldChanged(levels[num].WorldId);
		}
		if (num != -1 && updateUserPreferences && levels[num].AbsoluteLevelId >= 2)
		{
			UserPreferences.instance.playerData.progress = levels[num].AbsoluteLevelId;
			UserPreferences.instance.Save();
		}
	}

	private void InitializeLevelsProgress()
	{
		if (!CryptedFileSaver.IsFileExists(Application.persistentDataPath + progressFileName))
		{
			CreateNewLevelsProgress();
		}
		else
		{
			LoadLevelProgress();
		}
	}

	private void LoadLevelProgress()
	{
		List<Level> list = CryptedFileSaver.LoadFileWithBinaryFormater<List<Level>>(Application.persistentDataPath + progressFileName);
		if (list != null)
		{
			levels = list;
		}
		else
		{
			CreateNewLevelsProgress();
		}
	}

	private void CreateNewLevelsProgress()
	{
		Level level = new Level(1);
		level.IsLocked = false;
		levels.Add(level);
		SaveLevelProgress();
	}

	public void SaveLevelProgress()
	{
		SaveLocal();
	}

	protected void SaveLocal()
	{
		DebugUtil.Log("Saving Progress");
		CryptedFileSaver.SaveFileWithBinaryFormater(Application.persistentDataPath + progressFileName, levels);
	}

	public Level GetLevel(int absoluteLevelId)
	{
		for (int i = 0; i < levels.Count; i++)
		{
			if (levels[i].AbsoluteLevelId == absoluteLevelId)
			{
				return levels[i];
			}
		}
		return null;
	}

	public void SetAndSaveLevelProgress(int absoluteLevelId, byte howManyStars, bool crown)
	{
		if (howManyStars > 3)
		{
			howManyStars = 3;
		}
		bool flag = false;
		Level level = GetLevel(absoluteLevelId);
		if (level.Stars < howManyStars)
		{
			level.Stars = howManyStars;
			flag = true;
		}
		if (!level.HaveCrown && crown)
		{
			level.HaveCrown = true;
			flag = true;
		}
		if (!level.PlayedThisLevel)
		{
			AnalyticsController.LevelCompleted(absoluteLevelId);
			level.PlayedThisLevel = true;
			flag = true;
			CheckAndUnlockNextLevel(updateUserPreferences: false);
		}
		if (flag)
		{
			UILevelsPicker uILevelsPicker = Object.FindObjectOfType<UILevelsPicker>();
			if (uILevelsPicker != null)
			{
				uILevelsPicker.OnWorldChanged(level.WorldId);
			}
			SaveLevelProgress();
		}
	}

	public List<Level> GetAllLevels()
	{
		return levels;
	}

	public List<Level> GetLevelsInWorlds(int worldId)
	{
		List<Level> list = new List<Level>();
		for (int i = 0; i < levels.Count; i++)
		{
			if (levels[i].WorldId == worldId)
			{
				list.Add(levels[i]);
			}
		}
		return list;
	}

	public Level GetLastUnlockedLevel()
	{
		for (int num = levels.Count - 1; num >= 0; num--)
		{
			if (!levels[num].IsLocked)
			{
				return levels[num];
			}
		}
		return null;
	}
}
